package com.jrzh.mvc.service.sys.impl;

import java.io.File;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jrzh.common.exception.ProjectException;
import com.jrzh.common.tools.CollectionTools;
import com.jrzh.common.utils.DateUtil;
import com.jrzh.config.ProjectConfigration;
import com.jrzh.framework.base.convert.BaseConvertI;
import com.jrzh.framework.base.repository.BaseRepository;
import com.jrzh.framework.base.service.impl.BaseServiceImpl;
import com.jrzh.framework.bean.SessionUser;
import com.jrzh.mvc.convert.sys.FileConvert;
import com.jrzh.mvc.model.sys.FileModel;
import com.jrzh.mvc.repository.sys.FileRepository;
import com.jrzh.mvc.search.sys.FileSearch;
import com.jrzh.mvc.service.sys.FileServiceI;
import com.jrzh.mvc.view.sys.FileView;

@Service("fileService")
public class FileServiceImpl extends
		BaseServiceImpl<FileModel, FileSearch, FileView> implements
		FileServiceI {

	@Autowired
	private FileRepository fileRepository;

	@Autowired
	ProjectConfigration projectConfigration;

	@Override
	public BaseRepository<FileModel, Serializable> getDao() {
		return fileRepository;
	}

	@Override
	public BaseConvertI<FileModel, FileView> getConvert() {
		return new FileConvert();
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void deleteFile(FileModel model, SessionUser user)
			throws ProjectException {
		String url = model.getUrl();
		if (StringUtils.isNotBlank(url)) {
			url = url.replace(projectConfigration.mappingUrl,
					projectConfigration.getFileRootUrl());
			File file = new File(url);
			if (file.exists()) {
				while (true) {
					if (null != file && file.exists()) {
						file.delete();
					} else {
						break;
					}
				}
			}
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void deleteFileModelByKey(String key, SessionUser user)
			throws ProjectException {
		List<FileModel> fileList = this.selectByField("key", key);
		if(CollectionTools.isNotBlank(fileList)){
			Set<FileModel> fileSet = new HashSet<FileModel>();
			fileSet.addAll(fileList);
			this.deletes(fileSet, user);
		}
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void deleteModel(FileModel model, SessionUser user)
			throws ProjectException {
		getDao().delete(model.getId());
	}
	
	//获取上传图片的地址
	@Override
	public String[] getUploadFilePath(String model) throws ProjectException{
		String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
		String ctxPath = projectConfigration.getFileRootUrl()+"file/" + model+ "/" + dateDir +"/";
		String mapPath = projectConfigration.mappingUrl+"file/" + model+ "/" + dateDir+"/";
		File file = new File(ctxPath);
		if(!file.exists()) file.mkdirs();
		String [] uploadFilePath={ctxPath,mapPath};
		return uploadFilePath;
	}

}
